<template>
  <div class="fillcontain" >

    <div class="head-title"> <i class="el-icon-edit-outline">新增规则组</i></div>

    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px"  class="form-container">

      <el-row>
        <el-col :span="12">
          <el-form-item label="规则组标识" label-width="120px" prop="groupIdentify">
            <el-input v-model="dataForm.groupIdentify" placeholder="规则组标识" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规则组名称" label-width="120px" prop="groupName">
            <el-input v-model="dataForm.groupName" placeholder="规则组名称" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>

        <el-col :span="12">
          <el-form-item label="是否有效" label-width="120px" prop="isEffect">
            <el-switch
              v-model="dataForm.isEffect"
              active-color="#13ce66"
              inactive-color="#ff4949"
              on-value="1" on-text="是" off-text="否" off-value="0">
            </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="规则组类型" label-width="120px" prop="groupType">
            <el-select v-model="dataForm.groupType" filterable disabled placeholder="请选择">
              <el-option
                v-for="item in groupTypes"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="规则组描述" label-width="120px" prop="groupDesc">
        <el-input type="textarea" :rows="3" v-model="dataForm.groupDesc" disabled placeholder="规则组描述"></el-input>
      </el-form-item>

      <el-row>
        <el-form-item label="关联模型" label-width="120px">
          <el-checkbox-group v-model="checkEntity" disabled @change="handleCheckedChange">
            <el-col :span="12" v-for="entity in entitysProperties" :key="entity.entityId">
              <el-checkbox :label="entity.entityId+''" :key="entity.entityId">{{entity.label}}</el-checkbox>
            </el-col>
          </el-checkbox-group>
        </el-form-item>
      </el-row>
    </el-form>


    <div class="mb10 pl20" v-if="dataForm.groupType==='1'">
      <div class="content-title"><span>规则</span></div>
    </div>

    <div class="contain" v-if="dataForm.groupType==='1'">
      <!-- 列表 -->
      <el-table
        :data="dataList"
        border
        style="width: 100%; ">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row>
              <el-col :span="24">
                <el-card class="box-card">
                  <div class="rule_content">
                    <el-row>
                      <span class="txt1">如果</span>
                    </el-row>
                    <el-row v-for="(condition,cindex) in scope.row.ruleConditionInfos" :key="cindex">
                      <el-row v-if="cindex==0">
                        <el-col :span="20">
                          <span class="txt3">{{condition.conditionDesc}}</span>
                        </el-col>
                      </el-row>
                      <el-row v-else-if="cindex!=0">
                        <el-col :span="20">
                          <span class="txt2">并且 </span> <span class="txt5">{{condition.conditionDesc}}</span>
                        </el-col>
                      </el-row>
                    </el-row>
                    <el-row>
                      <span class="txt1">那么</span>
                    </el-row>
                    <el-row v-for="(action,aindex) in scope.row.ruleActionInfos" :key="aindex">
                      <el-row>
                        <el-col :span="20">
                          <span class="txt4">{{action.actionDesc}}</span>
                        </el-col>
                      </el-row>
                    </el-row>
                  </div>
                </el-card>
              </el-col>

            </el-row>
          </template>
        </el-table-column>
        <el-table-column
          prop="ruleInfo.ruleCode"
          header-align="center"
          align="center"
          width="180"
          label="规则编号">
        </el-table-column>
        <el-table-column
          prop="ruleInfo.ruleName"
          header-align="center"
          align="center"
          width="180"
          label="规则名称">
        </el-table-column>
        <el-table-column
          prop="ruleInfo.ruleDesc"
          header-align="center"
          align="center"
          label="规则描述">
        </el-table-column>
        <el-table-column
          prop="isEffect"
          header-align="center"
          align="center"
          label="状态">
          <template slot-scope="scope">
            {{scope.row.ruleInfo.isEffect | isEffectFilter}}
          </template>
        </el-table-column>
      </el-table>

    </div>


    <div class="mb10 pl20" v-if="dataForm.groupType==='2'">
      <div class="content-title"><span>决策表</span></div>
    </div>

    <div class="contain" v-if="dataForm.groupType==='2'">
      <!-- 列表 -->
      <el-table :data="tableDatas" style="width: 100%">
        <el-table-column
          v-for="(col,i) in conditionList"
          :property="col.decisionCode"
          :key="col.decisionCode"
          :index="i"
          :label="col.conditionDesc"
        >
          <template slot="header" slot-scope="scope">
            <span v-if="conditionList[scope.column.index].headType=='1'">{{scope.column.label}} (条件)</span>
            <span v-if="conditionList[scope.column.index].headType=='2'">{{scope.column.label}} (动作)</span>
          </template>
          <template slot-scope="scope">
            <el-input
              v-model="scope.row[scope.column.property].conditionDesc"
              readOnly ></el-input>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="content-footer">
      <el-button @click="toList">返回</el-button>
    </div>

  </div>
</template>

<script>
    import {groupDetail} from '@/api/version/version'
    import {entityList} from '@/api/baseConfig/entity'
    import constant from "@/api/constant";

    export default {
        data() {
            return {
                conditionList: [],//表头信息
                tableDatas: [],//决策表数据
                groupTypes: constant.ruleConfig.groupTypes,
                sourceType:null,
                addOrUpdateVisible:false,
                checkEntity: [],
                dataList: [],
                isDisabled: false,
                visible: false,
                dataForm: {
                    id: null,
                    groupName: '',
                    groupDesc: '',
                    groupIdentify: '',
                    remark: '',
                    isEffect: '1'
                },
                entitysProperties: [],
                dataRule: {
                    groupName: [
                        {required: true, message: '规则组名称不能为空', trigger: 'blur'}
                    ]
                },
                itemDataForm: {
                    itemData: []
                },
                page: {
                    pageIndex: 1,
                    pageSize: 10,
                    totalPage: 0
                }
            }
        },
        created() {
        },
        components:{
        },
        mounted(){
            let groupIdentify = this.$route.params.groupIdentify
            let groupVersion = this.$route.params.groupVersion
            this.sourceType = this.$route.params.type
            this.init(groupIdentify,groupVersion)
        },
        methods: {
            toList(){
                constant.clearTag(this)
                if(this.sourceType == 'audit'){
                    this.$router.push({name: constant.routerName.listAuditGroup})
                }else{
                    this.$router.push({name: constant.routerName.listVersionGroup})
                }

            },
            handleCheckedChange(data){
            },

            init(groupIdentify,groupVersion) {
                // console.log(groupIdentify)
                this.visible = true
                this.isDisabled = false
                this.entitysProperties = []
                this.checkEntity = []

                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.getEntityProperties()

                    if (groupIdentify) {
                        this.isDisabled = true
                        let params = {
                            groupIdentify: groupIdentify,
                            groupVersion: groupVersion,
                        }
                        this.$store.dispatch('loading/CHANGE_LOADING', true)
                        groupDetail(params).then(res => {
                            this.$store.dispatch('loading/CHANGE_LOADING', false)
                            if (res.data.data && res.data.resultCode === 0) {
                                this.dataForm = res.data.data.ruleSceneInfo
                                this.dataList = res.data.data.ruleInfoList
                                this.tableDatas = res.data.data.body
                                this.conditionList = res.data.data.head
                                res.data.data.relList.forEach(item => {
                                    this.checkEntity.push(item.entityId + '')
                                })
                            }
                        }).catch(err=>{
                            this.$store.dispatch('loading/CHANGE_LOADING', false)
                        })
                    }
                })
            },


            // 获取实体类的属性
            getEntityProperties() {
                entityList({}).then(res => {
                    if (res.data.data) {
                        this.entitysProperties = []
                        res.data.data.forEach(item => {
                            item['label'] = item.entityIdentify + ' -- ' + item.entityName
                            this.entitysProperties.push(item)
                        })
                    }
                })
            },

        },
        filters: {
            isEffectFilter: function (value) {
                let label = ''
                if (value === '0') {
                    label = '失效'
                } else if (value === '1') {
                    label = "有效"
                }
                return label
            }
        }
    }
</script>

<style lang="scss">

  .el-table__row .el-form-item__content {
    margin-left: 0 !important;
  }

  .el-table__row .el-form-item {
    margin-bottom: 0 !important;
  }
  .rule_content{
    /*border: 1px solid #606266;*/
    /*border-radius: 5px;*/
    padding: 10px;
    max-height: 200px;
    overflow: auto;
  }
  .rule_content .txt1{

  }
  .rule_content .txt2{
    color: #F76565;
    display: inline-block;
  }
  .rule_content .txt3{
    color: #10AC8D;
    text-indent: 38px;
    display: inline-block;
  }
  .rule_content .txt5{
    color: #10AC8D;
    display: inline-block;
  }
  .rule_content .txt4{
    color: #4991DA;
    text-indent: 38px;
    display: block;
  }
</style>
